package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName StackedBarChartDemo
 * @Description
 * @Author wangss
 * @date 2020.06.30 22:23
 * @Version 1.0
 */
public class StackedBarChartDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        StackedBarChart<String, Number> stackedBarChart = createChart();

        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(stackedBarChart);

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(hBox);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("位移");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();
    }

    private StackedBarChart<String, Number> createChart() {
        CategoryAxis x = new CategoryAxis();
        x.setLabel("x轴");

        NumberAxis y = new NumberAxis("y轴", 0, 100, 10);

        XYChart.Series<String, Number> xy1 = new XYChart.Series<>();
        xy1.setName("xy1");

        XYChart.Series<String, Number> xy2 = new XYChart.Series<>();
        xy2.setName("xy2");

        XYChart.Data<String, Number> data1 = new XYChart.Data<>("一", 10);
        XYChart.Data<String, Number> data2 = new XYChart.Data<>("二", 20);
        XYChart.Data<String, Number> data3 = new XYChart.Data<>("三", 30);
        XYChart.Data<String, Number> data4 = new XYChart.Data<>("四", 40);

        XYChart.Data<String, Number> data5 = new XYChart.Data<>("一", 25);
        XYChart.Data<String, Number> data6 = new XYChart.Data<>("二", 90);
        XYChart.Data<String, Number> data7 = new XYChart.Data<>("三", 23);
        XYChart.Data<String, Number> data8 = new XYChart.Data<>("四", 40);

        xy1.getData().addAll(data1, data2, data3, data4);
        xy2.getData().addAll(data5, data6, data7, data8);

        StackedBarChart<String, Number> stackedBarChart = new StackedBarChart<>(x, y);
        stackedBarChart.getData().addAll(xy1, xy2);

        return stackedBarChart;
    }
}
